/*
* @Author: chenggh
* @Date: 2025-06-16
* @Link to Problem : https://oj.haizeix.com/problem/360
*/
#include <iostream>
using namespace std;
int val[10][10] = {0};
int dp[20][10][10][10][10] = {0};
// 二位前缀和
int VAL(int i, int j, int k, int l){
	return val[k][l] - val[k][j - 1] - val[i - 1][l] + val[i - 1][j - 1];
}
int S(int x){
	return x * x;
}
int main(){
	int n; cin >> n;
	for(int i = 1; i <= 8; i++){
		for(int j = 1; j <= 8; j++){
			cin >> val[i][j];
			val[i][j] += val[i - 1][j] + val[i][j - 1] - val[i - 1][j - 1];
		}
	}
	// 初始化，一块的情况
	for(int i = 1; i <= 8; i++){
		for(int j = 1; j <= 8; j++){
			for(int k = i; k <= 8; k++){
				for(int l = j; l <= 8; l++){
					dp[1][i][j][k][l] = S(VAL(i, j, k, l));
				}
			}
		}
	}
	// 状态更新
	for(int t = 2; t <= n; t++){
		for(int i = 1; i <= 8; i++){
			for(int j = 1; j <= 8; j++){
				for(int k = i; k <= 8; k++){
					for(int l = j; l <= 8; l++){
						#define INF 0x7ffffff
						int ans = INF;
						// 1. 竖着切
						for(int c = j; c < l; c++){
							int val1 = dp[1][i][j][k][c] + dp[t - 1][i][c + 1][k][l];
							int val2 = dp[t - 1][i][j][k][c] + dp[1][i][c + 1][k][l];
							ans = min(ans, min(val1, val2));
						}
						// 2. 横着切
						for(int c = i; c < k; c++){
							int val3 = dp[1][i][j][c][l] + dp[t - 1][c + 1][j][k][l];
							int val4 = dp[t - 1][i][j][c][l] + dp[1][c + 1][j][k][l];
							ans = min(ans, min(val3, val4));
						}
						dp[t][i][j][k][l] = ans;
					}
				}
			}
		}
	}
	cout << dp[n][1][1][8][8] << endl;
	return 0;
}